/**
 * 
 */
package com.lanxigua.utils;

import java.net.URL;

import android.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.KeyEvent;
import android.widget.Toast;

/**
 * @author Administrator
 * 
 */
public class Tooltips {
	private final static String Url = "http://www.lanxigua.com/";
	private static ProgressDialog mProgressDlg = null;
	private Context context = null;

	public Tooltips(Context context) {
		this.context = context;
	}

	public ProgressDialog loading(String message) {
		ProgressDialog mpDialog = new ProgressDialog(this.context);
		mpDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置风格为圆形进度条
		mpDialog.setTitle("提示");// 设置标题
		// mpDialog.setIcon(R.drawable.icon);//设置图标
		mpDialog.setMessage(message);
		mpDialog.setIndeterminate(false);// 设置进度条是否为不明确
		mpDialog.setCancelable(true);// 设置进度条是否可以按退回键取消
		// mpDialog.setButton("确定", new DialogInterface.OnClickListener(){
		//
		// @Override
		// public void onClick(DialogInterface dialog, int which) {
		// dialog.cancel();
		//
		// }
		//
		// });
		// mpDialog.show();
		return mpDialog;
	}

	/**
	 * 显示悬浮的信息
	 * 
	 * @param context
	 *            上下文（this）
	 * @param message
	 *            提示的信息
	 */
	public final void info(String message) {
		Toast toast = Toast.makeText(this.context, message, Toast.LENGTH_LONG);
		// toast.setDuration(2000);
		toast.show();
	}

	/* 显示Dialog的method */
	public final void alert(String message) {
		this.alert("", message);
	}
	
	public final void alert(String title, String message){
		new AlertDialog.Builder(this.context).setTitle(title)
		.setMessage(message)
		.setNegativeButton(R.string.ok, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		}).show();
	}
	
	/**
	 * 显示loading窗口
	 * 
	 * @param self
	 */
	public void loading(final Activity self) {
		// Auto-generated method stub
		if (mProgressDlg != null) {
			mProgressDlg.dismiss();
			mProgressDlg = null;
		}
		mProgressDlg = new ProgressDialog(self);
		mProgressDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mProgressDlg.setMessage("请稍后...");
		mProgressDlg.setIndeterminate(false);
		mProgressDlg.show();
	}

	/**
	 * 关闭loading窗口
	 */
	public void loadingClose() {
		if (mProgressDlg != null) {
			mProgressDlg.dismiss();
			mProgressDlg = null;
		}
	}

	/**
	 * 显示退出窗口
	 * 
	 * @param self
	 */
	public void exit(final Activity self) {
		AlertDialog.Builder builder = new Builder(self);
		builder.setMessage("确定要退出吗？");
		builder.setTitle("提示");
		builder.setPositiveButton("确定",
				new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						self.finish();
						// android.os.Process.killProcess(android.os.Process.myPid());
					}
				});
	}

	/**
	 * 按回退键退出
	 * 
	 * @param keyCode
	 * @param event
	 * @param self
	 * @return
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event, final Activity self) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			exit(self);
			return true;
		}
		return true;
	}

	/**
	 * 网络读取图片成Bitmap
	 * 
	 * @param path图片路径
	 */
	public static final Bitmap getImageBitmap(String path) throws Exception {
		Bitmap bm = null;

		// String url = "http://www.lanxigua.com";
		URL requestUrl = new URL(Url + path);
		bm = BitmapFactory.decodeStream(requestUrl.openStream());

		return bm;
	}
}
